package com.frank.service.impl;

import com.frank.dao.InterviewRecordMapper;
import com.frank.dto.InterviewUsersDto;
import com.frank.model.InterviewRecord;
import com.frank.service.InterviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class InterviewServiceImpl implements InterviewService {

    @Resource
    private InterviewRecordMapper interviewRecordMapper;

    @Override
    public List<InterviewUsersDto> getUsersByInterviewId(Integer interviewId) {
        List<Map> mapList = interviewRecordMapper.selectUsersByInterviewId(interviewId);

        List<InterviewUsersDto> interviewUsersDtoList = new ArrayList<>();

        for (Map map : mapList){
            InterviewUsersDto interviewUsersDto = new InterviewUsersDto();
            interviewUsersDto.setInterviewRecordId((Integer)map.get("id"));
            interviewUsersDto.setUserId((Integer)map.get("user_id"));
            interviewUsersDto.setUser_name((String)map.get("real_name"));

            interviewUsersDtoList.add(interviewUsersDto);
        }

        return interviewUsersDtoList;
    }

    @Override
    public boolean attachInterview(Integer interviewRecordId) {
        InterviewRecord interviewRecord = new InterviewRecord();
        interviewRecord.setId(interviewRecordId);
        interviewRecord.setIsAttend(1);
        return interviewRecordMapper.updateByPrimaryKeySelective(interviewRecord) == 1;
    }

    @Override
    public boolean unAttachInterview(Integer interviewRecordId) {
        InterviewRecord interviewRecord = new InterviewRecord();
        interviewRecord.setId(interviewRecordId);
        interviewRecord.setIsAttend(0);
        return interviewRecordMapper.updateByPrimaryKeySelective(interviewRecord) == 1;
    }

    @Override
    public boolean finishInterview(InterviewRecord interviewRecord) {

        return interviewRecordMapper.updateByPrimaryKeySelective(interviewRecord) == 1;
    }
}
